from django.db import models

# Create your models here.

from django.contrib.auth.models import AbstractUser


class UserInfo(AbstractUser):

    nid = models.AutoField(primary_key=True)
    phone = models.CharField(max_length=11, null=True, unique=True, verbose_name="手机")
    avatar = models.FileField(upload_to="avatars/", default="avatars/default.png", verbose_name="头像")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    blog = models.OneToOneField(to="Blog", to_field="nid", verbose_name="个人博客")

    def __str__(self):
        return self.username

    class Meta:
        verbose_name = "用户"
        verbose_name_plural = verbose_name




class Blog(models.Model):

    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=64, verbose_name="博客标题")
    site = models.CharField(max_length=32, unique=True, verbose_name="博客后缀")
    theme = models.CharField(max_length=32, verbose_name="博客主题")

    def __str__(self):
        return str(self.title)

    class Meta:
        verbose_name = "blog站点"
        verbose_name_plural = verbose_name




class Category(models.Model):

    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=32, verbose_name="博客类别标题" )
    blog = models.ForeignKey(to="Blog", to_field="nid" , verbose_name="所属博客")

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "文章类别"
        verbose_name_plural = verbose_name




class Tag(models.Model):

    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=32, verbose_name="标签名")
    blog = models.ForeignKey(to="Blog", to_field="nid", verbose_name="所属博客")

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "标签"
        verbose_name_plural = verbose_name


class Article(models.Model):

    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=50, verbose_name="文章标题")
    desc = models.CharField(max_length=255, verbose_name="文章简介")
    create_time = models.DateTimeField(auto_now_add=True)

    # 评论数
    comment_count = models.IntegerField(verbose_name="评论数", default=0)
    # 点赞数
    up_count = models.IntegerField(verbose_name="点赞数", default=0)
    # 踩
    down_count = models.IntegerField(verbose_name="踩数", default=0)

    category = models.ForeignKey(to="Category", to_field="nid", null=True, verbose_name="文章类别")
    user = models.ForeignKey(to="UserInfo", to_field="nid", verbose_name="作者")
    tags = models.ManyToManyField(
        verbose_name="文章标签",
        to="Tag",
        through="Article2Tag",
        through_fields=("article", "tag"),  # 注意顺序！！！
    )

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "文章"
        verbose_name_plural = verbose_name



class ArticleDetail(models.Model):

    nid = models.AutoField(primary_key=True)
    content = models.TextField(max_length=10000, default="请输入文章内容", verbose_name="文章内容")
    article = models.OneToOneField(to="Article", to_field="nid", verbose_name="所属文章")

    class Meta:
        verbose_name = "文章详情"
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.article)





class Article2Tag(models.Model):

    nid = models.AutoField(primary_key=True)
    article = models.ForeignKey(to="Article", to_field="nid", verbose_name="文章")
    tag = models.ForeignKey(to="Tag", to_field="nid", verbose_name="标签")

    class Meta:
        unique_together = (("article", "tag"),)
        verbose_name = "文章-标签"
        verbose_name_plural = verbose_name

    def __str__(self):
        return '{0}({1})'.format(self.article, self.tag)


class ArticleUpDown(models.Model):

    nid = models.AutoField(primary_key=True)
    user = models.ForeignKey(to="UserInfo", null=True, verbose_name="用户")
    article = models.ForeignKey(to="Article", null=True, verbose_name="文章")
    is_up = models.BooleanField(default=True, verbose_name="是否点赞")

    class Meta:
        unique_together = (("article", "user"),)
        verbose_name = "文章点赞"
        verbose_name_plural = verbose_name

    def __str__(self):
        return '{0}({1})'.format(self.user, self.article)





class Comment(models.Model):
    
    nid = models.AutoField(primary_key=True)
    article = models.ForeignKey(to="Article", to_field="nid", verbose_name="文章")
    user = models.ForeignKey(to="UserInfo", to_field="nid",verbose_name="用户")
    content = models.CharField(max_length=255, verbose_name="评论内容")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="评论时间")
    parent_comment = models.ForeignKey("self", null=True, blank=True, verbose_name="二级评论")

    class Meta:
        verbose_name = "评论"
        verbose_name_plural = verbose_name

    def __str__(self):
        return '{0}({1})'.format(self.article, self.user)
